package edu.faytechcc.csc.gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * This class represents a single panel with a label, text field, and a button.
 * The button will open a file chooser window and when a file/folder is
 * selected, the file is displayed in the text field.
 * 
 * @author Charles Bryan
 */
public class FileChooserPanel extends JPanel
{

    private JLabel fileNameLabel;
    private JTextField fileNameTextField;
    private JButton browseButton;
    private final JFileChooser fc = new JFileChooser();

    public FileChooserPanel(String label, int textFieldSize)
    {
        buildPanel(label, textFieldSize);
    }      
    
    public FileChooserPanel(String label, int textFieldSize, 
            FileNameExtensionFilter filter)
    {
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        fc.setFileFilter(filter); 
        fc.setAcceptAllFileFilterUsed(false);
        buildPanel(label, textFieldSize);
    }   
    
    /**
     * Helper to set up the look and feel of the panel
     * @param label the text for the label to display
     * @param textFieldSize the size of the text field
     */
    private void buildPanel(String label, int textFieldSize)
    {
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.fileNameLabel = new JLabel(label);
        this.add(this.fileNameLabel);
        this.fileNameTextField = new JTextField(textFieldSize);
        this.fileNameTextField.addFocusListener(new TextFieldFocusListener());        
        this.add(this.fileNameTextField);
        this.browseButton = new JButton("Browse");
        this.browseButton.addActionListener(new BrowseButtonActionListener());
        this.add(this.browseButton);        
    }
    
    /**
     * Sets this Chooser Panel to allow the user to just select files, just 
     * select directories, or select both files and directories.
     * 
     * @param mode  the type of files to be displayed:
     *       <br />--JFileChooser.FILES_ONLY
     *       <br />--JFileChooser.DIRECTORIES_ONLY
     *       <br />--JFileChooser.FILES_AND_DIRECTORIES
     */
    public void setFileSectionMode(int mode)
    {
        fc.setFileSelectionMode(mode);
    }
    
    /**
     * Sets the current file filter. The file filter is used by the file 
     * chooser to filter out files from the user's view.
     * @param filter the new current file filter to use
     */
    public void setFileFilter(FileNameExtensionFilter filter)
    {
         fc.setFileFilter(filter);       
    }

    /**
     * Sets the current color of the text field to a warning color, true == on
     * == pink, false == off == white.
     * 
     * @param on true to turn on the warning color, false to turn it off.
     */
    public void setWarningColor(boolean on)
    {
        if (on)
        {
            fileNameTextField.setBackground(Color.PINK);
        }
        else
        {
            fileNameTextField.setBackground(Color.WHITE);
        }
    }

    /**
     * Checks to see if there currently is text in the text field.
     * 
     * @return true if the text field contains text, false otherwise.
     */
    public boolean isEmpty()
    {
        return fileNameTextField.getText().isEmpty();
    }

    /**
     * Returns the text found in the TextField.
     * 
     * @return the text contained in the text field.
     */
    public String getText()
    {
        return this.fileNameTextField.getText();
    }

    /**
     * Enables (or disables) the three components in this panel.
     * 
     * @param bool true to enable all of the components, otherwise false
     */
    @Override
    public void setEnabled(boolean bool)
    {
        super.setEnabled(bool);
        fileNameLabel.setEnabled(bool);
        fileNameTextField.setEnabled(bool);
        browseButton.setEnabled(bool);
    }

    private class BrowseButtonActionListener implements ActionListener
    {
        //open the file chooser when the button is clicked.
        @Override
        public void actionPerformed(ActionEvent e)
        {
            int returnVal = fc.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) //flase means canceled
            {
                File file = fc.getSelectedFile();
                fileNameTextField.setText(file.getPath());
                setWarningColor(false);
            }
        }
    }
    
    private class TextFieldFocusListener implements FocusListener
    {

        @Override
        public void focusGained(FocusEvent e)
        {
            
        }
        
        //If the text field is pink from a warning, turn the color white when
        //text is entered.
        @Override
        public void focusLost(FocusEvent e)
        {
            if(e.getSource() == fileNameTextField)
            {
                if(fileNameTextField.getBackground() == Color.PINK &&
                        !fileNameTextField.getText().isEmpty())
                {
                    fileNameTextField.setBackground(Color.WHITE);
                }
            }
        }        
    }
}
